<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.util.Date"%>
<%@ page import="org.apache.commons.io.*"%>
<%@ page import="org.apache.commons.lang3.*"%>
<%@ page import="java.nio.charset.Charset"%>
<%@ page import="java.sql.*"%>
<%@ page import="org.jsoup.Jsoup"%>
<%@ page import="org.jsoup.nodes.Document"%>
<%@ page import="org.jsoup.nodes.Element"%>
<%@ page import="org.jsoup.select.Elements"%>
<%@ page import="java.text.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<title>sql</title>
</head>
<body>
<%@ include file="mysql.jsp" %>
<%!
public String GbkToUtf8(String html){
	String a = "";
	try {
		File file = new File("target/noveltemp");
		FileUtils.writeStringToFile(file, html, Charset.forName("utf-8"));
		a = FileUtils.readFileToString(file, "utf-8");
	} catch (Exception e) {
	}
	return a;
}
public static Document getJsoupDocument(String url) throws Exception {
	long start = System.currentTimeMillis();
	Document doc = Jsoup.connect(url)
			// .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64;
			// rv:33.0) Gecko/20100101 Firefox/33.0")
			.userAgent("Mozilla").timeout(10000).cookie("auth", "token").get();
	long end = System.currentTimeMillis();
	System.out.println("getJsoupDocument time=" + (end - start));
	return doc;
}
%>

<%

try{
Document doc = getJsoupDocument("http://m.biquge.cm/wapbook/837.html");
Elements ele = doc.select(".chapter9 a");

Connection conn = getConnection();
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowdate = fmt.format(new Date());

for (int i = ele.size()-5; i >= 0; i--) {
	Element e = ele.get(i);
	String title = e.html();
	title = StringEscapeUtils.escapeHtml4(GbkToUtf8(title));
	Statement sql_statement = conn.createStatement();
	String query = "select count(*) c from article where title='"+title+"'";
	ResultSet result = sql_statement.executeQuery(query);
	result.last();
	out.println(title+"===在数据库中已经存在的数量："+result.getInt("c")+"<br />");
	if(result.getInt("c")==0){
		String url = e.attr("href");
		Document doc2 = getJsoupDocument("http://m.biquge.cm/"+url);
		String body = doc2.select("#nr1").first().html();
		body = body.replaceAll("找本站搜索\"笔趣阁CM\" 或输入网址:m.biquge.cm", "");
		body = StringEscapeUtils.escapeHtml4(GbkToUtf8(body));
		String sql1 = "insert into article(title, body, createdate) values('"+title+"', '"+body+"', '" + nowdate + "')";
		int flag = sql_statement.executeUpdate(sql1);
		out.println("<p style='color:red;'>"+title+" 更新状态："+flag+"</p>");
	}
	sql_statement.close();
	out.println("<hr />");
}
if (!conn.isClosed()) {%>
	System.out.println("数据库连接成功!");
	<%
}
conn.close();
}catch(Exception e){}
%>
<a href="index.jsp">
<div style="width:60%;height:100px;margin:5px auto;color:#FF7F50;background-color:#FF7F50;">
</div>
</a>

<br />

<input onclick="location.href='index.jsp'" type="button" value="go back" style="height:200px;width:400px;"/>
<br />
<a href="index.jsp" style="display:block;width:200px;height:100px;">go back</a>
</body>
</html>